<?php

class TravelAgencyField extends FormField {

	public function getDefaultName($model, $attribute) {
		$id = CHtml::value($model, $attribute);
		$model = TravelAgency::model()->findByPk($id);
		return $model ? $model->__toString() : '-请选择-';
	}
	
	public function renderInternal($model, $attribute) {
		
		$name = $this->getDefaultName($model, $attribute);
		
		$buttonId = 'hcs_'.uniqid();
		$labelId = $buttonId.'_label';
		$html = "<span id='{$labelId}'>{$name}</span>&nbsp;&nbsp;";
		$html .= $this->activeForm->hiddenField($model, $attribute, array(
			'id' => $buttonId . '_hide',
		));
		
		$button = CHtml::button('  选择  ', array(
			'class' => 'pn',
			'id' => $buttonId, 
		));
		$this->registerScript($buttonId);
		return $html.$button;
	}
	
	public function registerScript($id) {
		$hideId = $id.'_hide';
		$labelId = $id.'_label';
		$url = Yii::app()->createUrl('/admin/utils/hta/index');
		GridView::registerCssFile();
$js = <<<EOD
$('#{$id}').click(function() {
	$.dialog({
		'id': 'hcs',
		'title': '请选择旅行社',
		'padding': "10px 10px 0 10px"
	});
	dlg = $.dialog.get('hcs');
	dlg.dom.content.find('.grid-view a').live('click', function() {
		$.get(this.href, {}, function(result) {
			dlg.content(result);
		})
		return false;
	}).end()
	.find('form').live('submit', function() {
		$.get(this.action, $(this).serialize(), function(result) {
			dlg.content(result);
		});
		return false;
	}).end()
	.find('.hcs-btn-select').live('click', function() {
		$.getJSON($(this).attr('href'), {}, function(json) {
			$('#{$hideId}').val(json.id);
			$('#{$labelId}').html(json.name);
		});
		dlg.close();
		return false;
	});
	
	$.get('{$url}', {}, function(result) {
		dlg.content(result);
	});
});


EOD;
	
		$cs = Yii::app()->clientScript;
		$cs->registerScript($id, $js);

	}

}

?>